import app from "@/main";
import { NAVIGATE_TYPE } from "@/const/widget";

import store from "../store";
const callPhone = (phone) => {
  if (!phone) return;
  if (typeof phone != String) phone = phone.toString();
  uni.makePhoneCall({
    phoneNumber: phone,
  });
};
const lookLocation = (latitude, longitude, name, address) => {
  if (!latitude || !longitude) return;
  uni.openLocation({
    latitude,
    longitude,
    name,
    address,
    success: function () {
      console.log("success");
    },
  });
};
const toFullUrl = (value) => {
  if (!value) {
    return;
  }
  let fileBaseUrl = process.env.VUE_APP_BASE_FileUrl;

  if (
    value.startsWith("http") ||
    value.startsWith("/static/") ||
    value.startsWith("/pages/") ||
    value.startsWith("data:image")
  ) {
    return value;
  } else {
    if (!value.startsWith("/") && !value.startsWith("..")) {
      value = "/" + value;
    }
    return fileBaseUrl + value;
  }
};
const toTab = ({ key }) => {
  const CurrentPage = getCurrentPages();
  console.log(CurrentPage);
  store.commit("sp-common/base/SET_ACTIVE", key);
  uni.redirectTo({
    url: "/pages/home/index",
  });
};
const jumpPage = (path, params) => {
  // path   可以为name也可以为path    如果是back 默认是返回上一级
  // redirect = uni.reLaunch
  if (!path) return;
  if (!params?.redirect) {
    if (path.startsWith("/pages")) {
      app.$Router.push({
        path,
        query: params,
      });
    } else if (path.startsWith("back")) {
      app.$Router.back(params || 0);
    } else {
      app.$Router.push({
        name: path,
        params,
      });
    }
  } else {
    delete params.redirect;
    app.$Router.replaceAll({
      name: path,
      params,
    });
    if (path.startsWith("/pages")) {
      app.$Router.replaceAll({
        path,
        query: params,
      });
    } else {
      app.$Router.replaceAll({
        name: path,
        params,
      });
    }
  }
};
const transitionDistance = (m) => {
  if (!m) return;
  let n = "";
  if (m) {
    n = m >= 1000 ? `${(m / 1000).toFixed(2)}km` : `${m.toFixed(2)}m`;
  } else {
    n = "0m";
  }
  return n;
};
// 保留两位小数,不四舍五入
const toFixedNum = (v) => {
  let toStr = v.toString();
  let find = toStr.indexOf(".");
  let rs = "";
  if (find > -1) {
    // 截取两位小数点 或 补0
    let sp = toStr.split(".");
    let len = sp[1].toString().split("").length;
    if (len > 1) {
      rs = toStr.substring(0, find + 3);
    } else {
      rs = toStr + "0";
    }
  } else {
    rs = Number(v).toFixed(2);
  }
  return rs;
};

export const myTool = {
  callPhone,
  lookLocation,
  toFullUrl,
  jumpPage,
  toTab,
  transitionDistance,
  toFixedNum,
};

/*
params:
        type: ''--无,'self'--应用本身，'outside'--外联
        target: ，目
*/
export const callNavigateHook = ({ type, target }) => {
  switch (type) {
    case NAVIGATE_TYPE.SELF:
      uni.$u.route({
        url: target.path,
      });
      break;

    case NAVIGATE_TYPE.OUTSIDE:
      // #ifdef H5
      window.location.href = target.url;
      // #endif
      // #ifdef MP
      uni.$u.route({
        url: "pages/base/webview/index",
        params: {
          src: encodeURIComponent(target.url),
        },
      });
      // #endif
      break;

    case NAVIGATE_TYPE.SERVICE_CHAT:
      // #ifdef MP
      wx.openCustomerServiceChat({
        extInfo: {
          url: target.serviceChatUrl,
        },
        corpId: target.serviceChatID,
        success() {},
        fail() {
          uni.showToast({
            title: "当前无客服", //提示文字
            icon: "none",
            duration: 2000, //显示时长
          });
        },
      });
      // #endif
      break;

    default:
      break;
  }
};
